//
//  CZBaseSegmentedController.m
//  QQ空间
//
//  Created by zhangping on 16/1/20.
//  Copyright © 2016年 zhangping. All rights reserved.
//

#import "CZBaseSegmentedController.h"

@interface CZBaseSegmentedController ()

@end

@implementation CZBaseSegmentedController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupSegmentedControl];
}

/// 设置分段控件
- (void)setupSegmentedControl {
    
    // 如果子类没有实现这个方法来提供 UISegmentedControl 显示的 items 直接返回,不创建 UISegmentedControl
    if (![self respondsToSelector:@selector(itemsForBaseSegmentedController)]) {
        return;
    }
    
    NSArray *items = [self itemsForBaseSegmentedController];

    // 创建 UISegmentedControl 控件
    UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:items];
    seg.tintColor = CZCommonBGColor;
    
    self.navigationItem.titleView = seg;
    
    // 设置默认选中第0项,不会触发valueChanged事件
    seg.selectedSegmentIndex = 0;
    if ([self respondsToSelector:@selector(baseSegmentedControllerSelectedSegmented:)]) {
        [self baseSegmentedControllerSelectedSegmented:seg];
        
        // 设置segmented item选中事件
        [seg addTarget:self action:@selector(baseSegmentedControllerSelectedSegmented:) forControlEvents:UIControlEventValueChanged];
    }
}

@end
